#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#define High 18
#define Width 30
int pos_x;   //飞机位置
int pos_y;
int bullet_x, bullet_y;    //子弹位置
int bazi_x, bazi_y; //靶子
char c;
int score;
int i, j;
int speed;  //控制敌机速度
int level;


void gotoxy(int x,int y)  //光标移动到(x,y)位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

void HideCursor() // 用于隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};  // 第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}


void start (){
	score = 0;
	pos_x = Width / 2;   //飞机位置
	pos_y = High / 2;
	bullet_x = -1, bullet_y = -1;    //子弹位置
	bazi_x = Width / 2,bazi_y = 1; //靶子
	speed = 0;
	HideCursor(); // 隐藏光标

}


void user (){
	if(kbhit()){  // 判断是否有输入
		c = getch();
		if (c == 'w')
			pos_y--;
		else if (c == 's')
			pos_y++;
		else if (c == 'a')
			pos_x--;
		else if (c == 'd')
			pos_x++;
		else if (c == ' '){
			bullet_x = pos_x;
			bullet_y = pos_y - 3;
		}
		else if (c == 27){

			getch();
		}

	}
}


void show (){
	gotoxy(0,0);  // 光标移动到原点位置，以下重画清屏
	for (i = 1; i <= High; i ++){
		for (j = 1; j <= Width; j ++){
			if (i == pos_y && j == pos_x){                                //打印飞机
				printf ("*");
			}
			else if (i == pos_y && j == pos_x+1){
				printf ("*");
			}
			else if (i == pos_y && j == pos_x-1){
				printf ("*");
			}
			else if (i == pos_y-1 && j == pos_x){
				printf ("*");
			}
			else if (i == pos_y-2 && j == pos_x){
				printf ("*");
			}
			else if (i == pos_y-3 && j == pos_x){
				printf ("*");
			}
			else if (i == pos_y-2 && j == pos_x-1){
				printf ("*");
			}
			else if (i == pos_y-2 && j == pos_x+1){
				printf ("*");
			}
			else if (i == pos_y-2 && j == pos_x-2){
				printf ("*");
			}
			else if (i == pos_y-2 && j == pos_x+2){
				printf ("*");
			}
			else if (i == High){
				printf ("_");
			}
			else if (j == Width){
				printf ("|");
			}
			else if (i == bullet_y && j == bullet_x){
				printf ("|");
			}
			else if (i == bazi_y && j == bazi_x){
				printf ("@");
			}
			else
				printf (" ");
			
		}
		printf("\n");
		
	}
	printf ("分数：%d", score);
	
	
}




void update (){
	    level = 15 - score;
		bullet_y--;
		if (bullet_x == bazi_x && bullet_y == bazi_y){
			bazi_x = rand() % Width;
			bazi_y = 1;
			score++;
			bullet_y = -1;     //击中敌机消除子弹
		}

		speed ++;
		if (speed == level){
			bazi_y++;
			speed = 0;
		}
		if(bazi_y > High){
			bazi_x = rand() % Width;
			bazi_y = 1;
		}
		if (pos_y > High)                //防止飞机出界
			pos_y --;
		if (pos_x > Width)
			pos_x --;
		if (pos_x < 1)
			pos_x ++;
		if (pos_y < 1)
			pos_y ++;



}





int main(){
    start();
	while(1){
		show();
		user();
		update();

	}
	return 0;
}